<html>
<head>
    <script type="text/javascript" src="/js/jquery.min.js?v=202001010101"></script>
    <script type="text/javascript" src="/layui/layui.js?v=202001010101"></script>
    <script type="text/javascript" src="/js/app.js?v=202006010928"></script>
    <script>
        var socket;
        if ("WebSocket" in window) {
            //var ws = new WebSocket("ws://127.0.0.1:37777/scm/webscoket?userId=222");
            //由于无法添加请求头参数，故只能添加在url后面
            var token = getLocalStorage('TSF_SCM');
            var ws = new WebSocket("ws://test.scm.tsfyun.com:18888/websocket?token=" + token);
            //var ws = new WebSocket("ws://127.0.0.1:38686/webscoket?userId=222");
            socket = ws;
            ws.onopen = function() {
                alert('connection successfully');
            };
            ws.onmessage = function(evt) {
                var received_msg = evt.data;
                document.getElementById("showMes").value+=evt.data+"\n";
            };
            ws.onclose = function() {
                alert("close connection");
                //TODO 此处测试会发现自动断开连接，需采用心跳机制等等自动重连
            };
        } else {
            alert("explorer not support WebSocket");
        }
        function login(){
            var message=document.getElementById("name").value+":"+document.getElementById("mes").value;
            socket.send(message);
        }
    </script>
</head>
<body>
<textarea rows="3" cols="30" id="showMes" style="width:300px;height:500px;"></textarea>
<br/>
<label>name</label>
<input type="text" id="name"/>
<br/>
<label>message</label>
<input type="text" id="mes"/>
<button onclick="login();">send</button>
</body>
</html>
